package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.response.PageResult;
import com.itheima.health.response.Result;
import com.itheima.health.response.constant.CodeConstant;
import com.itheima.health.response.constant.MessageConstant;
import com.itheima.health.service.CheckItemService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:  RestFul 风格
 * @Version: V1.0
 * http://localhost:80/checkItem   查询  GET
 * http://localhost:80/checkItem   新增  POST
 * http://localhost:80/checkItem/{id}   删除 DELETE
 * http://localhost:80/checkItem   修改  PUT
 */
@RestController   // 组合注解：包含@Controller、@ResponseBody
@RequestMapping("checkItem")
public class CheckItemController {

    @Reference
    CheckItemService checkItemService;


    //@RequestMapping(value = "findAll", method = RequestMethod.GET)  // 支持的请求方式有几种？ 是否支持GET请求？
//    @GetMapping("findAll")
    @GetMapping
    public List<CheckItem> findAll() {
        List<CheckItem> list = checkItemService.findAll();
        return list;
    }


    /**
     * 根据ID查询
     * @param id 主键
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable("id") Integer id) {
        // 1 接收前端参数，JSON、k=v、url
        // 2 调用Service
        CheckItem checkItem = checkItemService.findOne(id);
        // 3 封装结果，响应前端
        return new Result(checkItem);
    }

    /**
     * 新增， 带主键返回
     * @param checkItem 检查项实体
     * @return
     */
    @PostMapping
    public Result add(@RequestBody CheckItem checkItem) {
        CheckItem item = checkItemService.add(checkItem);
        return new Result(item);
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        checkItemService.delete(id);
        return new Result(null);
    }


    /**
     * 更新
     * @param id  如果id不传， throw Exception
     * @param checkItem
     */
    @PutMapping("/{id}")
    public Result edit(@PathVariable("id") Integer id,@RequestBody CheckItem checkItem) {
        checkItemService.edit(id, checkItem);
        return new Result(null);
    }


    /**
     * 根据条件查询，带分页
     * @param checkItem 条件
     * @param page 当前页
     * @param size 一页显示记录数
     * @return
     */
    @PostMapping("/{page}/{size}")
    public Result search(@RequestBody CheckItem checkItem,
                                        @PathVariable("page") Integer page,
                                        @PathVariable("size") Integer size) {


        PageResult<CheckItem> result = checkItemService.search(checkItem, page, size);
        // 封装结果
        return new Result(CodeConstant.CHECKITEM_CODE_SUCCESS,
                MessageConstant.QUERY_CHECKITEM_SUCCESS, true, result);
    }


}
